﻿using System.Collections.Generic;
using System.IO;
using System.Text;

namespace KMLHelper
{
    /// <summary>
    /// 地名标注；
    /// 包含名称、描述、坐标、样式等信息，将输出在Placemark标签中；
    /// </summary>
    public class KMLPlacemark
    {
        private string name = null;
        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            set { this.name = value; }
        }

        private string description = null;
        /// <summary>
        /// 描述，用于输出属性信息
        /// </summary>
        public string Description
        {
            set { this.description = value; }
        }
        /// <summary>
        /// 属性信息（名称，值）
        /// </summary>
        public Dictionary<string, string> Properties
        {
            set {
                StringBuilder str = new StringBuilder();
                foreach (string key in value.Keys)
                {
                    str.AppendFormat("{0}: {1}</br>\r\n", key, value[key]);
                }
                if (str.Length > 0)
                {
                    int removeLength = "</br>\r\n".Length;
                    str.Remove(str.Length - removeLength, removeLength);
                }
                this.description = str.ToString();
            }
        }

        private KMLGeometry geometry = null;
        /// <summary>
        /// 坐标信息
        /// </summary>
        public KMLGeometry Geometry
        {
            set { this.geometry = value; }
        }

        private string address = null;
        /// <summary>
        /// 代表一个点所在位置或邮政编码的字符串；
        /// 标准地址格式：街道,城市,州；
        /// </summary>
        public string Address
        {
            set { this.address = value; }
        }

        public void Write(StreamWriter sw)
        {
            try
            {
                sw.WriteLine("<Placemark>");
                sw.WriteLine("<name>" + this.name + "</name>");
                if (string.IsNullOrEmpty(this.address) == false)
                {
                    sw.WriteLine("<address>" + this.address + "</address>");
                }
                if (string.IsNullOrEmpty(this.description) == false)
                {
                    sw.WriteLine("<description>\r\n<![CDATA[\r\n" + this.description + "\r\n]]>\r\n</description>");
                }

                this.geometry.Write(sw);

                sw.WriteLine("</Placemark>");
            }
            catch
            { throw; }
        }
    }
}
